package com.ciba.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciba.demo.mapper.UserInfoMapper;
import com.ciba.demo.pojo.UserInfo;
import com.ciba.demo.respone.ApiRestResponse;
import com.ciba.demo.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Repository
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public ApiRestResponse checkAccount(HttpServletRequest request, HttpServletResponse response) {
        String account = request.getParameter("account" );
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>() ;
        wrapper.eq(true , "email",account) ;
        UserInfo userInfo = userInfoMapper.selectOne(wrapper) ;
        if(userInfo!=null){
            return ApiRestResponse.success().setStatus(1).setMsg("有账户").setData(userInfo.getProfile_photo());
        }else{
            return ApiRestResponse.error().setStatus(0).setMsg("没有这个账户");
        }
    }

    @Override
    public ApiRestResponse checkUser(HttpServletRequest request, HttpServletResponse response) {
        String account = request.getParameter("account") ;
        String passWord = request.getParameter("passWord") ;
        Map<String,String> map = new HashMap<>() ;
        map.put("email",account) ;
        map.put("password",passWord) ;
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>() ;
        wrapper.allEq(map) ;
        wrapper.last("limit 1") ;
        UserInfo userInfo = userInfoMapper.selectOne(wrapper) ;
        if (userInfo!=null){
            request.getSession().setAttribute("user",userInfo);

            return ApiRestResponse.success().setStatus(1).setMsg("登陆成功！") ;
        }else{
            return ApiRestResponse.error().setStatus(0).setMsg("账号密码不匹配！");
        }
    }
}